package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;

    //处理超时未支付订单
    @Scheduled(cron = "0 0/1 * * * ?")//每分钟执行一次
    public void OverTimeNotPay(){
        System.out.println("定时器执行了"+LocalDateTime.now());
        LocalDateTime datetime=LocalDateTime.now().plusMinutes(-15);
        List<Orders> list= orderMapper.OverTimeNotPay(Orders.UN_PAID,datetime);
        if(!list.isEmpty()){
            for (Orders orders : list) {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelReason("支付超时");
                orders.setCancelTime(LocalDateTime.now());
                orderMapper.update(orders);
            }
        }
    }

    //处理已配送订单未确认完成订单
    @Scheduled(cron = "0 0 1 * * ?")//每天凌晨一点执行一次
    public void OverTimeNotConfirm(){
        LocalDateTime datetime=LocalDateTime.now().plusMinutes(-60);//处理前一天的订单
        List<Orders> list= orderMapper.OverTimeNotConfirm(Orders.DELIVERY_IN_PROGRESS,datetime);
        if(!list.isEmpty()){
            for (Orders orders : list) {
                orders.setStatus(Orders.COMPLETED);
                orders.setDeliveryTime(orders.getEstimatedDeliveryTime());
                orderMapper.update(orders);
            }
        }
    }
}
